{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# Managing a Fund of Funds\n",
    "\n",
    "If you have a Marquee portfolio with position tags, you can easily turn it into a fund of funds,\n",
    "with which you can create and leverage performance, risk, and thematic reports for a subset of portfolio\n",
    "positions based off their tags.\n",
    "\n",
    "This tutorial assumes you already have a portfolio with positions tags. To learn more about how to create a portfolio\n",
    "in Marquee using GS Quant, visit our tutorial\n",
    "[here](https://developer.gs.com/p/docs/services/portfolio/programmatic-access/upload/).\n",
    "\n",
    "## Step 1: Authenticate and Initialize Your Session\n",
    "\n",
    "First you will import the necessary modules and add your client id and client secret."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "\n",
    "from gs_quant.markets.portfolio_manager import PortfolioManager\n",
    "from gs_quant.session import GsSession\n",
    "\n",
    "client = None\n",
    "secret = None\n",
    "\n",
    "## External users must fill in their client ID and secret below and comment out the line below\n",
    "\n",
    "#client = 'ENTER CLIENT ID'\n",
    "#secret = 'ENTER CLIENT SECRET'\n",
    "\n",
    "GsSession.use(client_id=client, client_secret=secret)\n",
    "\n",
    "print('GS Session initialized.')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Step 2: Turn your portfolio into a fund of funds\n",
    "\n",
    "Let's imagine your portfolio has the following tags on its positions:\n",
    "\n",
    "| Tag Name | Possible Tag Values |\n",
    "|-----------------|---------------\n",
    "| `Sector`      | `Fintech` , `Banks` , `Software` |\n",
    "| `Analyst`     | `Marcus` , `Samuel` |\n",
    "\n",
    "When creating a fund of funds, the portfolio's tag name hierarchy is a list of the portfolio's\n",
    "tag names in an order that determines how the fund of funds is segmented. In this example, say we want to analyze\n",
    "the portfolio by `Sector`, and then `Analyst`:"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "pm = PortfolioManager(portfolio_id='ENTER PORTFOLIO ID HERE')\n",
    "pm.set_tag_name_hierarchy(['Sector', 'Analyst'])\n",
    "pm.update_fund_of_funds()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "The result will be the following list of sub-portfolios:\n",
    "\n",
    "| Sub-Portfolios |\n",
    "|-----------------\n",
    "| `Sector`: `Fintech`      |\n",
    "| `Sector`: `Banks`      |\n",
    "| `Sector`: `Software`      |\n",
    "| `Sector`: `Fintech` , `Analyst`: `Marcus`     |\n",
    "| `Sector`: `Banks` , `Analyst`: `Marcus`       |\n",
    "| `Sector`: `Software` , `Analyst`: `Marcus`       |\n",
    "| `Sector`: `Fintech` , `Analyst`: `Samuel`     |\n",
    "| `Sector`: `Banks` , `Analyst`: `Samuel`       |\n",
    "| `Sector`: `Software` , `Analyst`: `Samuel`       |\n",
    "\n",
    "You can retrieve this list as a dictionary using the following function:"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "print(pm.get_all_fund_of_fund_tags())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Step 3: Retrieve Sub-Portfolio Analytics\n",
    "\n",
    "Now we have a list of dictionaries in which each dictionary\n",
    "corresponds to a particular sub-portfolio. These dictionaries can be passed to retrieve the relevant\n",
    "reports for the sub-portfolio:"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "subportfolio_reports = pm.get_reports(tags={'Sector': 'Banks', 'Analyst': 'Marcus'})"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "From here, you can pull analytics from these reports as you would for any other portfolio. For more information\n",
    "on leveraging reports, visit our tutorials [here](https://developer.gs.com/p/docs/services/portfolio/programmatic-access/reports/).\n",
    "\n",
    "At any point, if you create/edit/delete reports on your main portfolio or change the tag name hierarchy on\n",
    "the portfolio, it's important to update your fund of funds so all your sub-portfolio reports are in sync\n",
    "with your main portfolio reports. You can do so by running this line:"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "pm.update_fund_of_funds()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "*Other questions? Reach out to the [Portfolio Analytics team](mailto:gs-marquee-analytics-support@gs.com)!*\n"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}